<?php

namespace common\models\ofc;

use Yii;
use common\helpers\Html;
use common\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%ofc_match}}".
 *
 * @property int $id
 * @property string|null $title 比赛名称
 * @property int $host_team_id 主队球队ID
 * @property string|null $host_team_name 主队球队
 * @property int $guest_team_id 客队球队ID
 * @property string|null $guest_team_name 客队球队
 * @property int $host_score 主队比分
 * @property int $guest_score 客队比分
 * @property int $match_time 比赛时间
 * @property int $first_time 开球时间
 * @property int $second_time 半场开球时间
 * @property int $season_id 赛季ID
 * @property string|null $group 小组
 * @property int $round 轮次
 * @property string|null $venue 比赛场馆
 * @property string|null $location 比赛地点
 * @property int $match_status 比赛状态
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 */
class OfcMatch extends \common\db\ActiveRecord {

    use \common\traits\SearchForBuildQuery;

    const STATUS_DEFAULT = 0;
    const STATUS_ACTIVE = 10;
//0 比赛异常
//1 未开赛
//2 上半场
//3 中场
//4 下半场
//5 加时赛
//6 加时赛
//7 点球决战
//8 完场
//9 推迟
//10 中断
//11 腰斩
//12 取消
//13 待定
    const MATCH_STATUS_DEFAULT = 0;
    const MATCH_STATUS_WAIT = 1;
    const MATCH_STATUS_FIRST_HALF = 2;
    const MATCH_STATUS_MIDDLE = 3;
    const MATCH_STATUS_SECOND_HALF = 4;
    const MATCH_STATUS_FIRST_OVERTIME = 5;
    const MATCH_STATUS_SECOND_OVERTIME = 6;
    const MATCH_STATUS_PENALTY = 7;
    const MATCH_STATUS_COMPLETED = 8;
    const MATCH_STATUS_DELAY = 9;
    const MATCH_STATUS_INTERRUPT = 10;
    const MATCH_STATUS_SCRAPPED = 11;
    const MATCH_STATUS_CANCEL = 12;
    const MATCH_STATUS_UNDETERMINED = 13;
    //
    const CURRENT_MATCH_CACHE_PREFIX = 'CurrentMatchId';

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%ofc_match}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['host_team_id', 'guest_team_id', 'host_score', 'guest_score', 'match_time', 'first_time', 'second_time', 'season_id', 'round', 'match_status', 'status', 'created_at', 'updated_at'], 'integer'],
            [['title', 'host_team_name', 'guest_team_name'], 'string', 'max' => 100],
            [['group', 'location'], 'string', 'max' => 50],
            [['venue'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'title' => '比赛名称',
            'host_team_id' => '主队球队ID',
            'host_team_name' => '主队球队',
            'guest_team_id' => '客队球队ID',
            'guest_team_name' => '客队球队',
            'host_score' => '主队比分',
            'guest_score' => '客队比分',
            'match_time' => '比赛时间',
            'first_time' => '开球时间',
            'second_time' => '半场开球时间',
            'season_id' => '赛季ID',
            'group' => '小组',
            'round' => '轮次',
            'venue' => '比赛场馆',
            'location' => '比赛地点',
            'match_status' => '比赛状态',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }

    public function getHostTeam() {
        return $this->hasOne(OfcTeam::className(), ['id' => 'host_team_id']);
    }

    public function getGuestTeam() {
        return $this->hasOne(OfcTeam::className(), ['id' => 'guest_team_id']);
    }

    public function getGroupText() {
        return $this->group_id ? chr(ord('A') + $this->group_id - 1) . '组' : '';
    }

    public function getRoundText() {
        return $this->round_id ? '第' . $this->round_id . '轮' : '';
    }

    public function getSeason() {
        return $this->hasOne(OfcSeason::class, ['id' => 'season_id']);
    }

    public function getCompleted() {
        return $this->match_status > static::MATCH_STATUS_WAIT && $this->match_status <= static::MATCH_STATUS_COMPLETED;
    }

    /**
     * 比赛隶属信息面板
     * 显示分组、轮次、赛季、赛事信息
     */
    public function getMatchEventPanel() {
        $data = [];
        if ($this->group_id && $this->group != null) {
            $data['group'] = $this->group->name;
        }
        if ($this->round_id && $this->round != null) {
            $data['turns'] = $this->round->name;
        }
        if ($this->season_id && $this->season != null) {
            $options = ['class' => 'font-blue-madison'];
            $name = $this->season->name . '赛季';
            $data['season'] = Html::tag('small', $name, $options);
        }
        if (!empty($data)) {
            return implode("\n", $data);
        }
        return '--';
    }

    /**
     * 对战球队信息面板
     * 显示对战球队信息
     */
    public function getVersusTeamPanel() {
        $host = $this->hostTeam;
        $guest = $this->guestTeam;

        $hostBox = self::dumpTeam($host->logo, $host->abbr);
        $guestBox = self::dumpTeam($guest->logo, $guest->abbr);
        $versusBox = self::dumpVersus($this->completed, $this->host_score, $this->guest_score);

        return Html::tag('div', $hostBox . $versusBox . $guestBox, ['class' => 'widget-versus']);
    }

    /**
     * 输出格式化后的球队信息
     * @param string $logo 球队标志
     * @param string $name 球队名称
     * @return string 球队信息
     */
    public static function dumpTeam($logo, $name) {
        $img = Html::img($logo, [], true);
        $icon = Html::tag('span', $img, ['class' => 'widget-versus-icon']);
        $body = Html::tag('span', $name, ['class' => 'widget-versus-abbr']);
        return Html::tag('div', $icon . $body, ['class' => 'widget-versus-team']);
    }

    /**
     * 输出格式化后的球队比分信息
     * @param int $completed 是否完赛
     * @param int $hostGoals 主队进球数
     * @param int $guestGoals 客队进球数
     * @param string $default 默认显示内容
     * @return string 比分信息
     */
    public static function dumpVersus($completed, $hostGoals, $guestGoals, $default = 'VS') {
        $versus = $default;
        if ($completed) {
            $color = 'blue';
            if ($hostGoals > $guestGoals) {
                $color = 'green';
            } elseif ($hostGoals < $guestGoals) {
                $color = 'red';
            }
            $versus = Html::tag('span', $hostGoals . ':' . $guestGoals, ['class' => 'font-' . $color]);
        }
        return Html::tag('div', $versus, ['class' => 'widget-versus-stat']);
    }

    public function getCourt() {
        if ($this->location && date('Y', $this->match_time) == '2020') {
            return $this->location;
        }
        $data = [];
        if ($this->location) {
            $data[] = $this->location;
        }
        if ($this->venue_id && $this->venue) {
            $data[] = $this->venue->name;
        } else {
            if ($this->hostTeam && $this->hostTeam->venue_id) {
                $venue = $this->hostTeam->venue;
                if ($venue) {
                    $data[] = $venue->name;
                    $this->venue_id = $venue->id;
                    $this->save(false);
                }
            }
        }
        return implode('·', array_filter($data));
    }

    public function getFullName() {
        if ($this->title) {
            return [$this->title];
        }
        $data = [];
        if ($this->season) {
            $data[] = $this->season->eventName;
        }
        if ($this->group_id) {
            $data[] = $this->groupText;
        }
        if ($this->round_id) {
            $data[] = $this->roundText;
        }
        return array_filter($data);
    }

    public function getName() {
        return implode('', $this->fullName);
    }

    public function getMatchStatus() {
        $text = [
            ' VS ',
            '未开赛',
            '上半场',
            '中场',
            '下半场',
            '加时赛',
            '加时赛',
            '点球决战',
            '已结束',
            '推迟',
            '中断',
            '腰斩',
            '取消',
            '待定'
        ];
        return [
            'status' => $this->match_status,
            'text' => ArrayHelper::getValue($text, $this->match_status, '--'),
        ];
    }

    public function getIsCompleted() {
        if ($this->match_status == self::MATCH_STATUS_COMPLETED) {
            return true;
        }
        return false;
    }

    public function getMatchTime() {
        return date('m-d H:i', $this->match_time);
    }

}
